//
//  RegisterViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/20.
//

import UIKit

class RegisterViewController: BaseViewController {

    private var totalTime: Int = 60
    private var timer: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavi()
        
        setupUI()
        
    }
    private func setupNavi() {
        navigationItem.title = "注册"
    }
    private func setupUI() {
        view.addSubviews(usernameTextField, usernameTipLabel, passwordTextField, twoPasswordTextField, countryView, phoneTextField, codeTextField, confirmButton)
        usernameTextField.snp.makeConstraints { make in
            make.top.equalTo(43)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(60)
        }
        usernameTipLabel.snp.makeConstraints { make in
            make.top.equalTo(usernameTextField.snp.bottom).offset(4)
            make.left.equalTo(22)
            make.right.equalTo(-22)
        }
        passwordTextField.snp.makeConstraints { make in
            make.top.equalTo(usernameTipLabel.snp.bottom).offset(20)
            make.left.equalTo(usernameTextField.snp.left)
            make.right.equalTo(usernameTextField.snp.right)
            make.height.equalTo(usernameTextField.snp.height)
        }
        twoPasswordTextField.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(20)
            make.left.equalTo(passwordTextField.snp.left)
            make.right.equalTo(passwordTextField.snp.right)
            make.height.equalTo(passwordTextField.snp.height)
        }
        countryView.snp.makeConstraints { make in
            make.top.equalTo(twoPasswordTextField.snp.bottom).offset(20)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(twoPasswordTextField.snp.height)
        }
        phoneTextField.snp.makeConstraints { make in
            make.top.equalTo(countryView.snp.bottom).offset(20)
            make.left.equalTo(twoPasswordTextField.snp.left)
            make.right.equalTo(twoPasswordTextField.snp.right)
            make.height.equalTo(twoPasswordTextField.snp.height)
        }
        codeTextField.snp.makeConstraints { make in
            make.top.equalTo(phoneTextField.snp.bottom).offset(20)
            make.left.equalTo(phoneTextField.snp.left)
            make.right.equalTo(phoneTextField.snp.right)
            make.height.equalTo(phoneTextField.snp.height)
        }
        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(codeTextField.snp.bottom).offset(28)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(50)
        }
    }
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    @objc private func confirmButtonAction() {
        let isUserName = usernameTextField.text?.isEmpty ?? false
        let isPassword = passwordTextField.text?.isEmpty ?? false
        let isTwoPassword = twoPasswordTextField.text?.isEmpty ?? false
        let isPhone = phoneTextField.text?.isEmpty ?? false
        let isCode = codeTextField.text?.isEmpty ?? false
        let isSample = passwordTextField.text == twoPasswordTextField.text
        
        if isUserName {
            Toast.with(message: "请输入用户名").showOnWindow()
            return
        }
        if isPassword {
            Toast.with(message: "请输入密码").showOnWindow()
            return
        }
        if isTwoPassword {
            Toast.with(message: "请输入密码").showOnWindow()
            return
        }
        if isPhone {
            Toast.with(message: "请输入手机号").showOnWindow()
            return
        }
        if isCode {
            Toast.with(message: "请输入验证码").showOnWindow()
            return
        }
        if isSample {
            Toast.with(message: "两次输入的密码不相等").showOnWindow()
            return
        }
        Toast.with(message: "注册成功").showOnWindow()
        navigationController?.popViewController(animated: true)
    }
    @objc private func getNumButtonAction () {
        
        getNumButton.isUserInteractionEnabled = false
        timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { [weak self] timer in
            if let number = self?.totalTime, number > 0 {
                self?.getNumButton.setTitle("\(number)s", for: .normal)
                self?.getNumButton.setTitle("\(number)s", for: .selected)
                self?.totalTime -= 1
            }else {
                self?.getNumButton.setTitle("获取验证码", for: .normal)
                self?.getNumButton.setTitle("获取验证码", for: .selected)
                self?.timer?.invalidate()
                self?.timer = nil
                self?.getNumButton.isUserInteractionEnabled = true
            }
        })
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var usernameTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入用户名", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var usernameTipLabel: UILabel = {
        let label = UILabel()
        label.text = "用户名由字母开头.数字、字母、下划线组成，长度为6-32位之间."
        label.textColor = .lxs.hex("#E5E5E5")
        label.font = .lxs.font(size: 12)
        label.numberOfLines = 0
        return label
    }()
    private lazy var passwordTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入密码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var twoPasswordTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "确认密码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var countryView: CustomCountryNumView = {
        let country = CustomCountryNumView()
        return country
    }()
    private lazy var phoneTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入手机号", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var codeTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "请输入验证码", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#808080"), NSAttributedString.Key.font: UIFont.lxs.font(size: 14)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 14)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = passwordTextFieldRightView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var getNumButton: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 20, y: 0, width: 70, height: 60)
        button.setTitle("获取验证码", for: .normal)
        button.setTitle("获取验证码", for: .selected)
        button.setTitleColor(.lxs.hex("#57A0FF"), for: .normal)
        button.setTitleColor(.lxs.hex("#57A0FF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 14)
        button.addTarget(self, action: #selector(getNumButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var passwordTextFieldRightView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 111, height: 60))
        view.addSubview(getNumButton)
        return view
    }()
    private lazy var confirmButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setTitle("注册", for: .normal)
        button.setTitle("注册", for: .selected)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .normal)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(confirmButtonAction), for: .touchUpInside)
        return button
    }()
}
